const mysql = require('../db');
const {generatorToken} = require("../utils/token")

const {
  GraphQLString,
  GraphQLInt,
  GraphQLObjectType,
  GraphQLList,
  GraphQLSchema,
  GraphQLID,
  GraphQLNonNull
} = require('graphql');

// 第一步: 根据数据库数据库表已经有的字段定义可供前端可以查询的字段
const AccountSchema = new GraphQLObjectType({
  name: 'account',
  fields: {
    id: {
      type: GraphQLID,
    },
    username: {
      type: GraphQLString,
    },
    password: {
      type: GraphQLString,
    },
    created_at: {
      type: GraphQLString,
    },
    updated_at: {
      type: GraphQLString,
    }
  }
});

// 定义登录后的返回类型
const LoginResSchema = new GraphQLObjectType({
  name: 'loginRes',
  fields: {
    code: {
      type: GraphQLInt,
    },
    message: {
      type: GraphQLString,
    },
    token: {
      type: GraphQLString,
    }
  }
})

const RootSchema = new GraphQLObjectType({
  name: 'root',
  fields: {
    // 查询列表出来
    accountList: {
      type: GraphQLList(AccountSchema),
      async resolve(parent, args) {
        const result = await mysql.query('select id, username, password,created_at, updated_at from account');
        console.log(result);
        return result;
      }
    },
    // 根据id查询出一条数据
    account: {
      type: AccountSchema,
      args: {id: {type: GraphQLID}},
      async resolve(parent, args) {
        console.log(args, '参数')
        const result = await mysql.query('select id, username, password,created_at, updated_at from account where id = ?', [args.id]);
        return result[0];
      }
    }
  }
})

const MutationSchema = new GraphQLObjectType({
  name: 'mutation',
  fields: {
    addAccount: {
      // type表示返回值类型
      type: GraphQLString,
      args: {
        username: {
          // GraphQLNonNull表示不能为空，在文档上会体现出!
          type: new GraphQLNonNull(GraphQLString),
        },
        password: {
          type: new GraphQLNonNull(GraphQLString),
        }
      },
      async resolve(parent, args) {
        const username = args.username;
        const password = args.password;
        const result = await mysql.query('insert into account(username, password) values(?,?)', [username, password]);
        console.log(result, '插入数据');
        if (result && result.affectedRows) {
          return '插入成功';
        } else {
          return '插入失败';
        }
      }
    },

    // 根据id删除数据
    deleteAccountById: {
      type: GraphQLString,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID)
        }
      },
      async resolve(parent, args) {
        const result = await mysql.query('delete from account where id = ?', [args.id]);
        if (result.affectedRows) {
          return '删除成功';
        } else {
          return '删除失败';
        }
      }
    },
    // 根据id修改数据
    modifyAccountById: {
      type: GraphQLString,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID)
        },
        username: {
          type: GraphQLString
        },
        password: {
          type: GraphQLString,
        }
      },
      async resolve(parent, args) {
        const id = args.id;
        const username = args.username;
        const password = args.password;
        const result = await mysql.query('update account set username = ?, password = ? where id = ?', [username, password, id]);
        if (result && result.affectedRows) {
          return '修改成功';
        } else {
          return '修改失败';
        }
      }
    },

    // 用户登录
    login: {
      type: LoginResSchema, // 登录后返回的类型
      args: {
        username: {
          type: new GraphQLNonNull(GraphQLString),
        },
        password: {
          type: new GraphQLNonNull(GraphQLString),
        },
      },
      async resolve(parent, args) {
        console.log(parent, '======>')
        const username = args.username;
        const password = args.password;
        const result = await mysql.query('select * from account where username =? and password = ?', [username, password]);
        console.log(result[0], '登录结果')
        if (result && result.length) {
          return {
            code: 0,
            message: '登录成功',
            token: generatorToken(result[0]),
          };
        } else {
          return {
            code: 1,
            message: '用户名或密码错误',
            token: '',
          };
        }
      }
    }
  }
})

module.exports = new GraphQLSchema({
  query: RootSchema,
  mutation: MutationSchema,
})
